package com.bnyy.medicalHousekeeper.wxapi;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;

import com.blankj.utilcode.util.ImageUtils;
import com.bnyy.common.Constant;
import com.bnyy.medicalHousekeeper.BuildConfig;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;
import java.io.File;


/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/8/17
 **/
public class WXManager {

    public static void shareMiniProgram(Context context, String miniProgramId, String miniProgramPath, String title, String image) {
        ProgressDialog progressDialog = ProgressDialog.show(context, "", "请稍后...");
        WXMiniProgramObject miniProgramObj = new WXMiniProgramObject();
        miniProgramObj.webpageUrl = "http://www.qq.com"; // 兼容低版本的网页链接
        miniProgramObj.miniprogramType = RequestManager.getInstance().isFormal() ? WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE : WXMiniProgramObject.MINIPROGRAM_TYPE_TEST;// 正式版:0，测试版:1，体验版:2
        miniProgramObj.userName = miniProgramId;     // 小程序原始id
        miniProgramObj.path = miniProgramPath;            //小程序页面路径；对于小游戏，可以只传入 query 部分，来实现传参效果，如：传入 "?foo=bar"
        WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
        msg.title = title;                    // 小程序消息title
        SendMessageToWX.Req req = new SendMessageToWX.Req();

        Glide.with(context)
                .load(GlideHelper.getGlideUrl(image))
                .error(R.mipmap.default_1_1)
                .placeholder(R.mipmap.default_1_1)
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        progressDialog.dismiss();
                        msg.thumbData = ImageUtils.compressByQuality(ImageUtils.drawable2Bitmap(resource), 32);
                        req.message = msg;
                        req.scene = SendMessageToWX.Req.WXSceneSession;  // 目前只支持会话

                        IWXAPI api = WXAPIFactory.createWXAPI(context, Constant.WX.APP_ID);
                        api.sendReq(req);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                        progressDialog.dismiss();
                        msg.thumbData = ImageUtils.compressByQuality(ImageUtils.drawable2Bitmap(placeholder), 32);
                        req.message = msg;
                        req.scene = SendMessageToWX.Req.WXSceneSession;  // 目前只支持会话

                        IWXAPI api = WXAPIFactory.createWXAPI(context, Constant.WX.APP_ID);
                        api.sendReq(req);
                    }
                });
    }

    public static void shareImage(Context context, File image, int scene) {
        IWXAPI api = WXAPIFactory.createWXAPI(context, Constant.WX.APP_ID);
        WXMediaMessage msg = new WXMediaMessage();
        WXImageObject imgObj = new WXImageObject();
        if (api.getWXAppSupportAPI() >= 0x27000D00) {
            Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", image);
            // 授权给微信访问路径
            context.grantUriPermission("com.tencent.mm",  // 这里填微信包名
                    contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

            imgObj.imagePath = contentUri.toString();
        } else {
            imgObj.imagePath = image.getAbsolutePath();
        }
        msg.mediaObject = imgObj;

        //设置缩略图
        Bitmap bitmap = ImageUtils.getBitmap(image);
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 160, 320, true);
        bitmap.recycle();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        thumbBmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        thumbBmp.recycle();
        msg.thumbData = baos.toByteArray();

//        msg.thumbData = ImageUtils.compressByQuality(ImageUtils.getBitmap(image), 32 * 1024L);
        //构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "img_" + System.currentTimeMillis();
        req.message = msg;
//        req.scene = SendMessageToWX.Req.WXSceneSession;
        req.scene = scene;
        api.sendReq(req);
    }


}
